function record3 = merge(record1,record2)
% merge the simulation results of multiple runs
record3 = SinkToFile();
assert(all(record1.time==record2.time),'Cannot merge to records for different time');
r1 = record1.repeat;
r2 = record2.repeat;
record3.time = record1.time;
record3.mean = (record1.mean*r1+record2.mean*r2)/(r1+r2);
record3.var = ((record1.var+record1.mean.^2)*r1+(record2.var+record2.mean.^2)*r2)/(r1+r2)-(record3.mean).^2;
record3.repeat = record1.repeat+record2.repeat;
end